var contactsArr = [];
var groupList = [];
var structureList = [];
var staffList = [];
var structureTitle = {"orgName": "", "orgID": 1};
const contactsPage = Vue.component('vrv-contacts', {
    template: "#contacts",
    data: function () {
        return {
            head: "通讯录",
            keywords: "",
            letter_flag: false,
            letter: 'A',
            contactsChecked: [],
            groupChecked: [],
            vrv_contacts_switch: 1,
        }
    },
    methods: {
        back_page:function(){
            if (this.vrv_contacts_switch == 3) {
                this.vrv_contacts_switch = 1;
            }
            else {
               backPage();
            }
        },
        go_group: function () {
            this.keywords = '';
            this.vrv_contacts_switch = 3;
        },
        keywords_search: function (val, keywords) {
            if (keywords == "") {
                return val;
            }
            else {
                var arr = [];
                val.forEach(function (e) {
                    if (e.userName.indexOf(keywords) > -1) {
                        arr.push(e)
                    }
                });
                return arr;
            }
        },
        index_to_letter: function (val) {
            var _this = this;
            _this.letter_flag = true;
            _this.letter = val;
            setTimeout(function () {
                _this.letter_flag = false;
            }, 600);
            var ele = document.getElementById('c' + val);
            ele.scrollIntoView(true);
        },
        structure_item_title: function (val, index) {
            var id = val.orgID;
            var arr = store.state.structureTitle;
            var arr1 = [];
            if (id === arr[arr.length - 1].orgID) {
                return false;
            }
            else {
                var data = {userName: val.orgName, userID: id};
                getStructure(data, true, true);
                for (var i = 0; i < index + 1; i++) {
                    arr1.push(arr[i]);
                }
                store.state.structureTitle = arr1;
            }
        },
        structure_check: function (val) {
            getStructure(val, false, true);
        },
        send_contacts_selected: function () {
            alert(JSON.stringify(store.state.contactsChecked));
            alert(JSON.stringify(store.state.groupChecked));
        },
        contacts_select:function(val){
            var arr = store.state.contactsChecked;
            var index;
            var flag = arr.some(function(e,i) {
                var f = parseInt(val.userID) == parseInt(e.userID);
                if (f){
                    index = i;
                }
                return f;
            });
            if (flag){
                store.state.contactsChecked.splice(index,1);
            }else {
                store.state.contactsChecked.push(val);
            }
        },
        group_select:function(val){
            var arr = store.state.groupChecked;
            var index;
            var flag = arr.some(function(e,i) {
                var f = parseInt(val.userID) == parseInt(e.userID);
                if (f){
                    index = i;
                }
                return f;
            });
            if (flag){
                store.state.groupChecked.splice(index,1);
            }else {
                store.state.groupChecked.push(val);
            }
        },
        init:function(){
            var _this = this;
            var arr = store.state.contactsChecked;
            var arr3 = store.state.groupChecked;
            if (arr.length > 0){
                var arr2 = [];
                arr.forEach(function(e){
                    arr2.push(e.userID);
                });
                _this.contactsChecked = arr2;
            }
            if (arr3.length > 0){
                var arr4 = [];
                arr3.forEach(function(e){
                    arr4.push(e.userID);
                });
                _this.groupChecked= arr4;
            }
            store.state.structureTitle[0].orgID = structureTitle.orgID;
            store.state.structureTitle[0].orgName = structureTitle.orgName;
            store.state.structureList = structureList;
            store.state.staffList = staffList;
        }
    },
    mounted: function () {
        this.init();
    }
});
function getStructure(val, flag, toast) {
    var fg = flag || false;
    var tf = toast || false;
    vrv.jssdk.getOrganization({
        orgId: val.userID,
        success: function (r) {
            var length1 = r.organization.length || 0;
            var length2 = r.staff.length || 0;
            if (length1 == 0 && length2 == 0) {
                if (tf) {
                    mui.toast('此组织下无人员');
                }
            }
            else {
                var ao = [];
                var as = [];
                if (length1 > 0) {
                    r.organization.forEach(function (v) {
                        ao.push({
                            userName: v.orgName,
                            userID: v.orgID,
                        });
                    });
                }
                if (length2 > 0) {
                    r.staff.forEach(function (n) {
                        var url = n.portraitUrl ? n.portraitUrl : '';
                        as.push({
                            userName: n.staffName,
                            userID: parseInt(n.staffID),
                            userPhotoUrl: url
                        });
                    });
                }
                store.state.structureList = ao;
                store.state.staffList = as;
                if (!fg) {
                    store.state.structureTitle.push({"orgName": val.userName, "orgID": val.userID});
                }
            }
        }
    });
}
var arrLetter = ['A', 'B', 'C', 'D', 'E', 'F', 'J', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
function JsonSort(json, key) {
    for (var j = 1, jl = json.length; j < jl; j++) {
        var temp = json[j],
            val = temp[key],
            i = j - 1;
        while (i >= 0 && json[i][key] > val) {
            json[i + 1] = json[i];
            i = i - 1;
        }
        json[i + 1] = temp;

    }
    return json;
};
function getFirstLetter(str) {
    var first = str.split('')[0];
    var str1 = checkCh(first).split('');
    var str2 = str1[str1.length - 1].toUpperCase();
    if (arrLetter.indexOf(str2) == -1) {
        str2 = '#';
    }
    return str2;
}
function contactsSort(arr) {
    var letter = [], dr = {};
    arr.forEach(function (v) {
        var l = v.letter;
        var a = {
            userName: v.userName,
            userID: v.userID,
            userPhotoUrl: v.userPhotoUrl
        };
        if (letter.indexOf(l) == -1) {
            letter.push(l);
            dr[l] = {letter: l, data: [a]};
        }
        else {
            dr[l].data.push(a);
        }
    });
    var na = [];
    letter.forEach(function (e) {
        na.push(dr[e])
    });
    var newArr = JsonSort(na, 'letter');
    return newArr;
}
